<template>

	<div>
		<el-dialog title="激活" :visible.sync="dialogTableVisible" width="30%" :close-on-click-modal="false"
			:show-close="false" :fullscreen="true">
			<div style="text-align: center;">
				<el-form ref="form" label-width="100px" style="width: 300px;margin: 100px auto;">
					<el-form-item label="激活方式">
						<el-select v-model="ValidityWay" placeholder="请选择激活方式" style="width: 100%;">
							<el-option label="激活码" value="1"></el-option>
							<el-option label="企业识别号" value="2"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="激活码" v-if="ValidityWay == '1'">
						<el-input v-model="cdKey" placeholder="请输入激活码" style="width: 100%;"></el-input>
					</el-form-item>
					<el-form-item label="企业识别号" v-if="ValidityWay == '2'" style="width: 100%;">
						<el-input v-model="cdKey" placeholder="请输入企业识别号"></el-input>
					</el-form-item>
					<div style="margin-top: 60px;text-align: center;">
						<el-button @click="confirmBind" type="primary">确认绑定</el-button>
						<el-button @click="logout()" type="danger" style="margin-left: 60px;">退出登录</el-button>
					</div>
				</el-form>
			</div>
		</el-dialog>
	</div>

</template>

<script>
	import {
		Session,
		Local
	} from '@/utils/storage.js';
	let that
	export default {
		name: 'headerCom',
		data() {
			return {
				dialogTableVisible: true,
				show1: false,
				cdKey: '',
				codeType: '',
				enterpriseInfo: {},
				ValidityWay: '1',

				ticketId: '',
				entDet: '',
				userInfo: '',
				params: {
					ticketId: ''
				}
			}
		},
		created: function() {},
		mounted: function() {
			that = this

			if (that.$store.state.token) {
				that.ticketId = that.$store.state.token
				if (localStorage.getItem("user")) that.userInfo = JSON.parse(localStorage.getItem("user"))
				that.getUserLicenceStatus()
			}
		},

		methods: {

			//获取激活状态
			getUserLicenceStatus() {
				that.ajax({
					url: 'licence/getUserLicenceStatus',
					data: {
						cdKey: that.cdKey
					},
					contentType: 'application/x-www-form-urlencoded',
					success: (res) => {
						if (res.data) {
							that.codeType = res.data
							if (res.data == "HAVE_ACTIVATED") that.$router.push("/home")
							// if (that.codeType == "NOT_ACTIVATED" || that.codeType == "EXPIRE_ADMIN_ACTIVATED")
							// 	return that.dialogTableVisible = true
						}
					},
				})
			},

			//确定绑定
			confirmBind() {
				if (!that.cdKey) return that.$message.error('请输入完整');

				that.$confirm('是否确认绑定该设备码?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					if (that.ValidityWay == '1') {
						that.ajax({
							url: 'licence/activeBbepLicence',
							data: {
								cdKey: that.cdKey
							},
							contentType: 'application/x-www-form-urlencoded',
							success: (res) => {
								if (res.data) {
									that.$message.success('激活成功！');
									setTimeout(function() {
										that.dialogTableVisible = false
										that.$router.push("/home")
									}, 1000)
								}
							},
						})
					}

					if (that.ValidityWay == '2') {
						that.ajax({
							url: 'licence/joinBbepEnt',
							data: {
								identityCode: that.cdKey
							},
							contentType: 'application/x-www-form-urlencoded',
							success: (res) => {
								if (res.data) {
									that.$message.success('激活成功！');
									setTimeout(function() {
										that.dialogTableVisible = false
										that.$router.go(0)
									}, 500)
								}
							},
						})
					}
				}).catch(() => {

				});
			},


			logout() {
				// 清除缓存/token等
				Session.clear();
				// 使用 reload 时，不需要调用 resetRoute() 重置路由
				this.$router.push("/login");
			}
		}
	}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
	.el-col-20 {
		height: 60px;
		line-height: 60px;
		overflow-y: scroll;
	}
</style>